﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;


namespace WpfHost
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        System.ServiceModel.ServiceHost host;
        public Window1()
        {
            InitializeComponent();
            this.button1.Click +=new RoutedEventHandler(button1_ClickCreate);
            ResfreshIHM();
        }

        void host_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
        {
            this.listBox1.Items.Add(e.Message);
        }

        void host_StateChanged(object sender, EventArgs e)
        {
            listBox1.Items.Add("Le host passe en mode :" + host.State.ToString());
            if (host.State == CommunicationState.Opened)
            {
                this.button1.Content = "Closing";
                this.button1.Click -= new RoutedEventHandler(button1_ClickClose);
                this.button1.Click -= new RoutedEventHandler(button1_ClickOpen);
                this.button1.Click -= new RoutedEventHandler(button1_ClickCreate);
                this.button1.Click += new RoutedEventHandler(button1_ClickClose);
            }
            else
            {
                this.button1.Content = "Openning";
                this.button1.Click -= new RoutedEventHandler(button1_ClickClose);
                this.button1.Click -= new RoutedEventHandler(button1_ClickOpen);
                this.button1.Click -= new RoutedEventHandler(button1_ClickCreate);
                this.button1.Click += new RoutedEventHandler(button1_ClickOpen);
            }
        }

        private void ResfreshIHM()
        {
            this.textBox1.Text = host.State.ToString();
            if (host.SingletonInstance != null)
                this.textBox3.Text = ((ServiceModel.Service1)host.SingletonInstance).NbAppel.ToString();
            else
                this.textBox3.Text = "0";
        }

        private void button1_ClickOpen(object sender, RoutedEventArgs e)
        {
            try
            {
                host.Open();
            }
            catch
            {
                this.listBox1.Items.Add("Plantage host.Open()");
            }
            ResfreshIHM();
        }
        private void button1_ClickClose(object sender, RoutedEventArgs e)
        {
            try
            {
                host.Close();
            }
            catch
            {
                host.Abort();
                this.listBox1.Items.Add("Plantage host.Close()");
            }
            ResfreshIHM();
        }
        private void button1_ClickCreate(object sender, RoutedEventArgs e)
        {
            try
            {
                host = new ServiceHost(typeof(ServiceModel.Service1));
                host.Closed += new EventHandler(host_StateChanged);
                host.Closing += new EventHandler(host_StateChanged);
                host.Faulted += new EventHandler(host_StateChanged);
                host.Opened += new EventHandler(host_StateChanged);
                host.Opening += new EventHandler(host_StateChanged);
                host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);
            }
            catch
            {
                this.listBox1.Items.Add("Plantage lors de la creation de l'Hôte");
            }
            ResfreshIHM();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ResfreshIHM();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.listBox1.Items.Clear();
            foreach (QCM.Contract.Categorie cat in ((ServiceModel.Service1)host.SingletonInstance).GetCategories())
            {
                this.listBox1.Items.Add(cat.Nom);
            }
        }
    }
}
